<?php namespace App\Admin\Orders\Orders;

//基础条件
use App\Core\Repository\Criterias\FixedCriteria;
use App\Core\Repository\Criterias\PaginateCriteria;
use App\Core\Repository\Criterias\SortCriteria;
//过滤器
use App\Core\Repository\Criterias\Filters\InputCriteria;
use App\Core\Repository\Criterias\Filters\InputKeywordsCriteria;
use App\Core\Repository\Criterias\Filters\InputTimeRangeCriteria;

use Input;
use App\Core\Exceptions\InputError;
use App\Admin\Logistics\Logistics\Repository as LogisticsRepository;
use App\Admin\Logistics\Delivers\Repository as DeliversRepository;

trait Service {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\All;
    use \App\Core\Service\Services\Indexer;
    use \App\Core\Service\Services\Creater;
    use \App\Core\Service\Services\Updater;
    use \App\Core\Service\Services\Changer;
    use \App\Core\Service\Services\Deleter;
    use \App\Core\Service\Services\Detailer;
    //操作日志
    use \App\Admin\Base\LogService;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
        //添加观察者
        $this->addListeners([
            new Listeners\DetailListener()
        ]);
    }
	
    //列表
    public function lists(){
        //过滤器
        $conditions = [
            new InputKeywordsCriteria('order_name,wxorder'),
            new InputTimeRangeCriteria('created_at'),
            new InputCriteria('invoice_status'),
            new InputCriteria('type'),
            new InputCriteria('status'),
            new InputCriteria('deliver_status')
        ];
        $count = $this->repository->addCriterias($conditions)->count();
        $conditions[] = new PaginateCriteria();
        $conditions[] = new SortCriteria('id', 'DESC');
        $data = $this->repository->resetQuery()->resetCriteria()->addCriterias($conditions)->all();
        $data->load('user', 'experience','delivers');
        return $this->success([
            'count' => $count,
            'data' => $data
        ]);
    }

    public function createlogistics(){
        $order_id = Input::get('order_id');
        $user_id = Input::get('user_id');
        $deliver_id = Input::get('deliver_code');
        $deliver_no = Input::get('deliver_no');

        $delivers = with(new DeliversRepository)->addCriteria(new FixedCriteria('id', $deliver_id))->first();
        $deliver_code=$delivers->code;
        if($deliver_no){
            $this->repository->addCriteria(new FixedCriteria('id', $order_id))->update([
                'deliver_status' => '1',
                'deliver_id' => $deliver_id,
                'deliver_code' => $deliver_code,
                'deliver_no' => $deliver_no,
            ]);
            $logisticsRepository = new LogisticsRepository();
            $model = $logisticsRepository->addCriteria(new FixedCriteria('order_id', $order_id))->first();
            if($model){
                $model->deliver_id = $deliver_id;
                $model->deliver_code = $deliver_code;
                $model->deliver_no = $deliver_no;
                $model->save();
            }else{
                $logisticsRepository->resetQuery()->create([
                    'order_id' => $order_id,
                    'user_id' => $user_id,
                    'deliver_id' => $deliver_id,
                    'deliver_code' => $deliver_code,
                    'deliver_no' => $deliver_no,
                ]);
            }
            
            return $this->success();
        }
        return $this->inputError('非法操作');
    }

    //订单完成
    public function complete(){
        return $this->change('status', '3', '2');
    }
}